 
package com.zdkj.subway.check.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.SwAuTvmShort;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmShortMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuTvmShortService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 售票员补款
 *
 * @author zhangxy
 * @date 2024-12-18 14:02:32
 */
@Service
public class SwAuTvmShortServiceImpl extends ServiceImpl<SwAuTvmShortMapper, SwAuTvmShort> implements SwAuTvmShortService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SwAuTvmShortMapper swAuTvmShortMapper;

    @Override
    public Result getSwAuTvmShortList(JSONObject jsonObject) {
        //售票员id
        Integer conductorPersonId = jsonObject.getInteger("conductorPersonId");
        String conductorPersonCode = jsonObject.getString("conductorPersonCode");//售票员工号
        //线路站点
        String lineCode = jsonObject.getString("lineCode");
        String stationCode = jsonObject.getString("stationCode");
        //收入时间
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Date recordTime = jsonObject.getDate("recordTime");
        Date supPaymentTime = jsonObject.getDate("supPaymentTime");
        //售票员确认状态
        //0：未确认
        //1：已确认
        String confirmStatus = jsonObject.getString("confirmStatus");
        //是否补款
        //1:补款
        //0:未补款
        String isSupPayment = jsonObject.getString("isSupPayment");
        String isNotice = jsonObject.getString("isNotice");
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");

        Example example = new Example(SwAuTvmShort.class);
        example.setOrderByClause("RECORD_TIME desc");
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(stationCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
        if(StringUtils.isEmpty(lineCode)) {
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            //管辖线路
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> LineObjects = new ArrayList<>();
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    lineCode = jsonObject1.getString("line");
                    LineObjects.add(lineCode);
                }
                criteria.andIn("lineCode", LineObjects);
            }else {
                //不管辖线路
                criteria.andEqualTo("lineCode", sysUser.getLineCode());
            }
        }
        if (conductorPersonId != null) {
            criteria.andEqualTo("conductorPersonId", conductorPersonId);
        }
        if (!StringUtils.isEmpty(conductorPersonCode)) {
            criteria.andLike("conductorPersonCode", conductorPersonCode);
        }
        if (!StringUtils.isEmpty(confirmStatus)) {
            criteria.andEqualTo("confirmStatus", confirmStatus);
        }
        if (!StringUtils.isEmpty(isSupPayment)) {
            criteria.andEqualTo("isSupPayment", isSupPayment);
        }

        if (startTime != null) {
            criteria.andGreaterThan("incomeSettleBeginTime",startTime);
        }
        if (endTime != null) {
            criteria.andLessThan("incomeSettleEndTime",endTime);
        }
        if (recordTime != null) {
            criteria.andEqualTo("recordTime",recordTime);
        }
        if (supPaymentTime != null) {
            criteria.andEqualTo("supPaymentTime",supPaymentTime);
        }
        criteria.andEqualTo("isNotice", "1");
        PageHelper.startPage(startPage,pageSize);
        List<SwAuTvmShort> swAuTvmShorts = swAuTvmShortMapper.selectByExample(example);
        PageInfo<SwAuTvmShort> swAuTvmShortPageInfo = new PageInfo<>(swAuTvmShorts);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list",swAuTvmShorts);
        resultJson.put("pageInfo",swAuTvmShortPageInfo);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    @Override
    public List<Map<String, Object>> getSwAuTvmShort(Map<String, String> param) {
        Map<String,Object> p1=new HashMap<>();
        String lineCode = param.get("lineCode");
        String stationCode = param.get("stationCode");
        if (StringUtils.isNotEmpty(lineCode) || StringUtils.isNotEmpty(stationCode)) {
            List<Map<String, Object>> maps = swAuTvmShortMapper.getSwAuTvmShort(param);
            return maps;
        }
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<Map<String,Object>> fareDifferencesInAudits=new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            param.put("lineCode",line);
            param.put("stationCode",station);
            List<Map<String,Object>> reportErrorAlterAppayOne = swAuTvmShortMapper.getSwAuTvmShort(param);
            fareDifferencesInAudits.addAll(reportErrorAlterAppayOne);
        }
        return fareDifferencesInAudits;
    }
}
